package com.lizhao.ai.impl.composite;

import com.lizhao.ai.common.EStatus;
import com.lizhao.ai.abs.BaseComposite;
import com.lizhao.ai.ifs.IBehaviour;
import com.lizhao.ai.ifs.composite.ISequence;

import java.util.Iterator;

/**
 * 选择器（或节点 有一个成功就返回成功，所有失败就是返回失败）
 */
public class SelectorImpl extends BaseComposite implements ISequence {

    @Override
    public EStatus update() {
        Iterator<IBehaviour> iterator = getChildren().iterator();
        if (iterator.hasNext()) {
            while (true) {
                IBehaviour currChild = iterator.next();
                EStatus s = currChild.tick();
                if (s != EStatus.Failure) {
                    return s;
                }
                //全部都是失败
                if (!iterator.hasNext()) {
                    return EStatus.Failure;
                }
            }
        }
        return EStatus.Invalid;  //循环意外终止
    }
}
